package com.bzsg.rightsManagementSystem.dao

import com.bzsg.rightsManagementSystem.pojo.Role
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface RoleDao {
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("select * from role where id = #{id}")
    fun queryById(id: Int): Role?

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Select("select * from role limit #{offset}, #{limit}")
    fun queryAllByLimit(@Param("offset") offset: Int, @Param("limit") limit: Int): List<Role>?

    /**
     * 通过实体作为筛选条件查询
     *
     * @param role 实例对象
     * @return 对象列表
     */
    @Select(" select * from role")
    fun queryAll(): List<Role>?

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 影响行数
     */
    @Insert(
        "   insert into role(role_code, role_name, role_description)\n" +
                "        values (#{roleCode}, #{roleName}, #{roleDescription})"
    )
    fun insert(role: Role)

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 影响行数
     */
    @Update(
        "   <script>\n" +
                "        update role\n" +
                "        <set>\n" +
                "            <if test=\"roleCode != null and roleCode != ''\">\n" +
                "                role_code = #{roleCode},\n" +
                "            </if>\n" +
                "            <if test=\"roleName != null and roleName != ''\">\n" +
                "                role_name = #{roleName},\n" +
                "            </if>\n" +
                "            <if test=\"roleDescription != null and roleDescription != ''\">\n" +
                "                role_description = #{roleDescription}\n" +
                "            </if>\n" +
                "        </set>\n" +
                "        where id=#{id}\n" +
                "        </script>"
    )
    fun update(role: Role)

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    @Delete("delete from role where id = #{id}")
    fun deleteById(id: Int)

    /**
     * selectRolesByUserId
     *
     * @param userId 主键
     * @return 角色列表
     */
    @Select(
        " select r.*\n" +
                "        from role as r,\n" +
                "             user_role_relation AS ur\n" +
                "        where ur.user_id = #{userId}\n" +
                "          and r.id = ur.role_id"
    )
    fun selectRolesByUserId(userId: Int): List<Role>?

    /**
     * 系统角色数目
     *
     * @return 系统角色数量
     */
    @Select(
        "  SELECT COUNT(*)\n" +
                "        FROM role"
    )
    fun selectTotalRole(): Int
}